package com.taotao.controller;

import com.taotao.pojo.ContentCategory;
import com.taotao.service.ContentCategoryService;
import com.taotao.util.ExceptionUtil;
import com.taotao.util.TaotaoResult;
import com.taotao.util.model.EasyUITreeModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 * describe:内容分类Controller
 * @author hong.zheng
 * @date 2018/09/12
 */
@Controller
@RequestMapping("/content/category")
public class ContentCategoryController {

    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    @Autowired
    private ContentCategoryService contentCategoryService;

    @RequestMapping("/list")
    //设置相应的内容为json数据
    @ResponseBody
    public List<EasyUITreeModel> getContentCategoryList(@RequestParam(value="id", defaultValue="0")long parentId) throws Exception {
        //查询商品列表
        List<ContentCategory> contentCategories = contentCategoryService.getContentCategoryList(parentId);
        List<EasyUITreeModel> resultList = new ArrayList<EasyUITreeModel>();
        for (ContentCategory contentCategory : contentCategories) {
            //创建一个节点
            EasyUITreeModel node = new EasyUITreeModel();
            node.setId(contentCategory.getId().intValue());
            node.setText(contentCategory.getName());
            node.setState(contentCategory.getParent()?"closed":"open");

            resultList.add(node);
        }
        return resultList;
    }
    @RequestMapping("/create")
    @ResponseBody
    public TaotaoResult createContentCategory(Long parentId, String name) {
        try {
            contentCategoryService.insertContentCategory(parentId, name);
        }catch (Exception e)
        {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
        return TaotaoResult.ok();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public TaotaoResult deleteContentCategory(Long parentId, long  id) {
        try {
            contentCategoryService.deleteContentCategory(id,parentId);
        }catch (Exception e)
        {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
        return TaotaoResult.ok();
    }

    @RequestMapping("/update")
    @ResponseBody
    public TaotaoResult updateContentCategory(Long id, String name) {
        try {
            ContentCategory contentCategory = contentCategoryService.selectContentCategory(id);
            contentCategory.setName(name);
            contentCategoryService.updateContentCategory(contentCategory);
        }catch (Exception e)
        {
            LOGGER.error(e.getMessage(),e);
            return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
        }
        return TaotaoResult.ok();
    }
}
